<?php
class Tourism_order_barter_model extends BZ_Model
{
    protected $table   = 'tourism_order_barter';
    protected $table_2 = 'tourism_order_product';

    public function total($param = array())
    {
        $this->db->from($this->table . ' AS tourism_order_barter');
        $this->db->join($this->table_2 . ' AS tourism_order_product', 'tourism_order_barter.order_product_id = tourism_order_product.order_product_id');
        if (!empty($param['status'])) {
            $this->db->where('tourism_order_barter.status', $param['status']);
        }
        if (!empty($param['barter_search'])) {
            $this->db->where("(`tourism_order_product`.`goods_name` LIKE '%{$param['barter_search']}%') OR (`tourism_order_barter`.`barter_id` = '{$param['barter_search']}')");
        }
        if (!empty($param['start_date'])) {
            $this->db->where('tourism_order_barter.created_at >', $param['start_date'] . ' 00:00:00');
        }
        if (!empty($param['end_date'])) {
            $this->db->where('tourism_order_barter.created_at <', $param['end_date'] . ' 23:59:59');
        }
        $this->db->where('tourism_order_barter.seller_uid', $param['seller_uid']);
        $result = $this->db->get();
        return $result->num_rows();
    }

    public function page_list($page_num, $num, $param = array())
    {
        $this->db->select('tourism_order_barter.*, tourism_order_product.goods_image, tourism_order_product.goods_name, tourism_order_product.attribute_value, tourism_order_product.number AS buy_number, tourism_order_product.barter_num, tourism_order_product.refund_num, tourism_order_product.shop_price, tourism_order_product.pay_amount');
        $this->db->from($this->table . ' AS tourism_order_barter');
        $this->db->join($this->table_2 . ' AS tourism_order_product', 'tourism_order_barter.order_product_id = tourism_order_product.order_product_id');
        if (!empty($param['status'])) {
            $this->db->where('tourism_order_barter.status', $param['status']);
        }
        if (!empty($param['barter_search'])) {
            $this->db->where("(`tourism_order_product`.`goods_name` LIKE '%{$param['barter_search']}%') OR (`tourism_order_barter`.`barter_id` = '{$param['barter_search']}')");
        }
        if (!empty($param['start_date'])) {
            $this->db->where('tourism_order_barter.created_at >', $param['start_date'] . ' 00:00:00');
        }
        if (!empty($param['end_date'])) {
            $this->db->where('tourism_order_barter.created_at <', $param['end_date'] . ' 23:59:59');
        }
        $this->db->where('tourism_order_barter.seller_uid', $param['seller_uid']);
        $this->db->order_by('tourism_order_barter.created_at', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    public function examine($param = array())
    {
        $this->db->select('tourism_order_barter.*, tourism_order_product.goods_image, tourism_order_product.goods_name, tourism_order_product.attribute_value, tourism_order_product.number AS buy_number, tourism_order_product.barter_num, tourism_order_product.refund_num, tourism_order_product.shop_price, tourism_order_product.pay_amount');
        $this->db->from($this->table . ' as tourism_order_barter');
        $this->db->join($this->table_2 . ' AS tourism_order_product', 'tourism_order_barter.order_product_id = tourism_order_product.order_product_id');
        $this->db->where('barter_id', $param['barter_id']);
        return $this->db->get();
    }
    
    public function updataExamineStatus($params)
    {
        $data = array(
            'reject_content' => !empty($params['reject_content']) ? $params['reject_content'] : '',
            'verify_time'    => date('Y-m-d H:i:s', time())
        );
        if (!empty($params['status'])) {
            $data['status'] = $params['status'];
        }
        if (!empty($params['logistics_company'])) {
            $data['logistics_company'] = $params['logistics_company'];
        }
        if (!empty($params['logistics'])) {
            $data['logistics'] = $params['logistics'];
        }
        $this->db->where('barter_id', $params['barter_id']);
        return $this->db->update($this->table, $data);
    }
}